﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using uTrade.Core;

namespace uTrade.Controls
{
    public class TimeLine : DrawObjBase
    {
        public DataSeries Date;
        public DataSeries Open;
        public DataSeries Close;
        public DataSeries High;
        public DataSeries Low;

        protected override int TotalItemCount { get => Date.Count; }

        /// <summary>
        /// 绘制下方时间轴
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        protected override void DrawObj(DrawingContext dc)
        {
            double left = ChartStartX;
            double top = ChartStartY;
            double height = ChartHeight;

            //每条记录的宽度
            var itemOffset = this.ChartWidth / TotalItemCount;

            //draw time line
            var yearLast = 0;
            for (var i = 0; i < TotalItemCount; i++)
            {
                var xoffset = (int)(i * itemOffset) + left + 0.5;
                var year = Convert.ToDateTime(Date[i]).Year;//(int)(priceInfo.PriceList[i].Date / 10000);
                if (year > yearLast)
                {
                    //格式文本，指定位置
                    FormattedText txt = new FormattedText(year.ToString(),
                     System.Globalization.CultureInfo.CurrentCulture,
                     FlowDirection.LeftToRight, new Typeface("Verdana"),
                     12, new SolidColorBrush(Color.FromRgb(64, 64, 64)));
                    //绘制文本
                    dc.DrawText(txt, new Point(xoffset, top + height - 16));
                }
                yearLast = year;
            }

            //draw curve line
            var highest = High.Max();
            var lowest = Low.Min();

            var pixelcount = 3;
            var inc = (int)(TotalItemCount * pixelcount / this.ChartWidth);
            if (inc == 0)
            {
                inc = 1;
            }

            //var color = "#30b8f3";
            var pen = GetPen(48, 184, 243, 1);
            //draw line curve.
            PathFigure pf = new PathFigure();
            PathGeometry pg = new PathGeometry();
            pg.Figures.Add(pf);

            for (var i = 0; i < TotalItemCount; i += inc)
            {
                var xoffset = (int)(i * itemOffset) + 0.5 + left;
                var yClose = (int)((highest - Close[i]) / (highest - lowest) * (height - 12)) + 0.5 + top;

                if (i == 0)
                {
                    pf.StartPoint = new Point(xoffset, yClose);
                }
                else
                {
                    pf.Segments.Add(new LineSegment(new Point(xoffset, yClose), true));
                }
            }

            dc.DrawGeometry(Brushes.Transparent, pen, pg);

            //draw item time region button
            double x = (int)(drawItemStartIndex * itemOffset) + left + 0.5;
            var xwidth = (int)(drawItemCount * itemOffset);

            var brush = Brushes.LightCyan;
            pen = new Pen(Brushes.LightCyan, 1);
            dc.DrawRectangle(brush, pen, new Rect(x, top, xwidth, height));
        }
    }
}